<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 15px;">
      <el-breadcrumb-item :to="{ path: '/' }">
        <i class="el-icon-s-promotion"></i> 后台管理
      </el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/sys-admin/product/attributeTemplate' }">属性模版管理</el-breadcrumb-item>
      <el-breadcrumb-item>添加属性模版</el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider></el-divider>

    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="名称" prop="name">
        <el-input v-model="ruleForm.name"></el-input>
      </el-form-item>
      <el-form-item label="拼音" prop="pinyin">
        <el-input v-model="ruleForm.pinyin"></el-input>
      </el-form-item>
      <el-form-item label="关键词" prop="keywords">
        <el-input v-model="ruleForm.keywords"></el-input>
      </el-form-item>
      <el-form-item label="排序序号" prop="sort">
        <el-input v-model="ruleForm.sort"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">添加</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
        <el-button type="warning" @click="$router.push('/sys-admin/product/attributeTemplate')">返回列表</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        ruleForm: {
          name: '测试属性模版001',
          pinyin:'pinyin',
          keywords: '测试属性模版的简介001',
          sort: '99',
        },
        rules: {
          name: [
            {required: true, message: '请输入名称', trigger: 'blur'},
            {min: 2, max: 25, message: '长度在 2 到 25 个字符', trigger: 'blur'}
          ],
          pinyin: [
            {required: true, message: '请输入简介', trigger: 'blur'}
          ],
          keywords: [
            {required: true, message: '请输入简介', trigger: 'blur'}
          ],
          sort: [
            {required: true, message: '请输入排序序号', trigger: 'blur'},
            {pattern:/^([0-9]|[1-9][0-9])$/, message: '排序序号必须是0~99之间的数值', trigger: 'blur'}
          ]
        }
      };
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            let url="http://176.212.26.33:8088/attributeTemplate/insert"
            console.log(url)
            let jwt=localStorage.getItem('jwt');

            this.axios.create({
              'headers':{
                'Authorization': jwt
              }
            }).post(url,this.qs.stringify(this.ruleForm)).then((response)=> {
              console.log(response.data);
              if(response.data.state==2000){
                this.$message.success("添加成功!");
                this.resetForm(formName);
              }else {
                this.$message.error("添加失败!")
              }
            });
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    }
  }
</script>